package com.enqualcomm.kids.component.command;

import com.android.volley.BuildConfig;
import com.android.volley.socket.SocketStack;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.Socket;

import common.utils.MyLogger;

/**
 * Created by yinzhiqun on 2015/10/16.
 *
 */
public class LoadPicCommand implements BaseCommand {
    private String userkey;
    private String userterminalid;
    private String path;
    private String ip;


    public LoadPicCommand(String userkey, String userterminalid, String path, String ip) {
        this.userkey = userkey;
        this.userterminalid = userterminalid;
        this.path = path;
        this.ip = ip;
    }

    @Override
    public int execute() {
        if (BuildConfig.Secret){
            return loadPicSecret();
        }else{
            return loadPicNormal();
        }
    }

    private int loadPicSecret() {
        Socket socket = null;
        try {
            {
                socket = new Socket(ip, 11647);
                socket.setSoTimeout(15 * 1000);

                byte[] userkeyBuffer = userkey.getBytes();
                byte[] userterminalidBuffer = userterminalid.getBytes();
                byte[] totalBuffer = new byte[49];
                totalBuffer[0] = 1;
                System.arraycopy(userkeyBuffer, 0, totalBuffer, 1, 32);
                System.arraycopy(userterminalidBuffer, 0, totalBuffer, 33, 16);

                SocketStack.send(socket, totalBuffer);
            }

            byte[] receive = SocketStack.receive(socket);

            socket.close();

            if (receive[0] == 1) {
                int imagelen = receive.length - 257;
//                if (imagelen == -256) {
                if (imagelen<0){
                    //没有数据
                    MyLogger.jLog().i("该用户没有上传过图片");
                    return -1;
                }

                FileOutputStream out = new FileOutputStream(new File(path));
                out.write(receive,257,imagelen);
                out.close();
                MyLogger.jLog().i("头像下载成功");
                return STATUS_OK;
            }else{
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        MyLogger.jLog().i("头像下载失败");
        return -2;
    }

    private int loadPicNormal() {
        Socket socket = null;
        DataOutputStream dao = null;
        FileOutputStream out = null;
        DataInputStream dis = null;
        try {

            socket = new Socket(ip, 11647);
            socket.setSoTimeout(15 * 1000);

            dao = new DataOutputStream(socket.getOutputStream());
            int len;

            byte[] buserkey = new byte[32];
            byte[] busertemid = new byte[16];
            len = 1 + 32 + 16;
            byte[] blen = new byte[4];
            byte[] sbuf = new byte[len + 4];

            for (int i = 3; i >= 0; i--) {
                byte b = (byte) ((len >> i * 8));

                blen[Math.abs(i - 3)] = b;
            }
            System.arraycopy(blen, 0, sbuf, 0, 4);
            sbuf[4] = 1;
            buserkey = userkey.getBytes();
            busertemid = userterminalid.getBytes();
            System.arraycopy(buserkey, 0, sbuf, 5, 32);
            System.arraycopy(busertemid, 0, sbuf, 5 + 32, 16);
            dao.write(sbuf);


            dis = new DataInputStream(socket.getInputStream());

            byte[] b2 = new byte[1024 * 100];
            int lens;

            byte[] belngth = new byte[4];
            lens = dis.read(belngth);

            int totlelen = ((belngth[0] & 0xff) << 24)
                    | ((belngth[1] & 0xff) << 16) | ((belngth[2] & 0xff) << 8)
                    | (belngth[3] & 0xff);
            byte[] bimage = new byte[totlelen];
            int acttotlelen = 0;
            while ((lens = dis.read(b2)) != -1) {
                System.arraycopy(b2, 0, bimage, acttotlelen, lens);
                acttotlelen += lens;

                if (acttotlelen == totlelen)
                    break;
            }
            socket.close();

            if (bimage[0] == 1) {
                int imagelen = bimage.length - 1 - 256;
                if (imagelen == -256) {
                    //没有数据
                    MyLogger.jLog().i("该用户没有上传过图片");
                    return -1;
                }

                byte[] bimagesrc = new byte[imagelen];
                System.arraycopy(bimage, 1 + 256, bimagesrc, 0, imagelen);

                out = new FileOutputStream(new File(path));
                out.write(bimagesrc);
                out.close();
                MyLogger.jLog().i("头像下载成功");
                return STATUS_OK;
            }else{
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        MyLogger.jLog().i("头像下载失败");
        return -2;
    }
}
